package com.ming.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ming.common.CodeMsg;
import com.ming.common.Constant;
import com.ming.common.Result;
import com.ming.common.realm.ActiveUser;
import com.ming.domain.BookE;
import com.ming.domain.SysUser;
import com.ming.from.BookForm;
import com.ming.from.SysUserForm;
import com.ming.mapper.SysUserMapper;
import com.ming.query.BookQuery;
import com.ming.query.SysUserQuery;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ming.mapper.BookMapper;
import com.ming.domain.Book;
import com.ming.service.BookService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.plugin.liveconnect.SecurityContextHelper;

/**
 *@Author:何元才
 *@Desc:
 *@data:2021-04-15 22:53
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService{


    @Autowired
    private BookMapper bookMapper;

    /**
     * 分页查询图书
     * @param query
     * @return
     */
    @Override
    public Result queryPage(BookQuery query) {
        //开启分页查询
        Page<Book> page = new Page<>(query.getPage(),query.getLimit());
        Page<Book> bookPage = bookMapper.selectPage(page, new LambdaQueryWrapper<Book>()
                .like(StringUtils.hasText(query.getBookName()),Book::getBookName,query.getBookName())
                .like(StringUtils.hasText(query.getAuthor()),Book::getAuthor,query.getAuthor())
                .eq(!ObjectUtils.isEmpty(query.getType()),Book::getType,query.getType())
                .eq(!ObjectUtils.isEmpty(query.getIsRent()),Book::getIsRent,query.getIsRent())
                .eq(!ObjectUtils.isEmpty(query.getDeleted()),Book::getDeleted,query.getDeleted())
        );
        return new Result(bookPage);
    }

    /**
     * 新增图书
     *
     * @param form
     * @return
     */
    @Override
    public Result add(BookForm form) {
        //当前用户
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        SysUser sysUser = activeUser.getSysUser();
        Integer userId = sysUser.getId();
        //创建一个书籍对象
        Book book = new Book();
        //组装数据
        BeanUtils.copyProperties(form,book);
        book.setIsRent(0);
        book.setVersion(1);
        book.setUserId(userId);
        book.setBookTime(new Date());
        book.setDeleted(0);
        bookMapper.insert(book);
        return new Result();
    }

    /**
     * 逻辑删除
     *
     * @param id
     * @return
     */
    @Override
    public Result del(Integer id) {
        //逻辑删除 修改就可以了  根据id找到当前选择的图书
        Book book = bookMapper.selectById(id);
        //无效
        if (book == null || book.getDeleted().equals(Constant.INVALID)) {
            return new Result(CodeMsg.USER_VALID_ERROR);
        }
        //设置失效
        book.setDeleted(1);
        bookMapper.updateById(book);
        return new Result();
    }

    /**
     * 检查入库
     *
     * @param bookId
     * @return
     */
    @Override
    public Result check(Integer bookId) {
        //找到选择的图书
        Book book = bookMapper.selectById(bookId);
        //检查图书状态 0 未检查入库的图书
        if (book.getIsRent() != 0 ){
            return new Result(CodeMsg.BOOK_SAVE_ERROR);
        }
        book.setIsRent(1);
        bookMapper.updateById(book);
        return new Result();
    }

}
